layui.use(['jquery', 'form', 'my_form', 'base64', 'laytpl'], function () {
    var $ = layui.jquery,
        my_form = layui.my_form,
        form = layui.form,
        base64 = layui.base64,
        laytpl = layui.laytpl;
    //
    $("#comment-list-box").data('data');
    //发表评论
    form.on('submit(form_submit)', function (data) {
        let options = {
            data: {data: base64.encode(my_form.stringify(data.field))},
            method: 'post',
            url: Config.comment_publish,
        }
        my_form.request(options, function (ret) {
            let tpl = $('#comment-list-tpl').html();
            $(".main-comment-list-nomore").remove();
            laytpl(tpl).render({data: ret}, function (html) {
                $("#main-comment-list .main-comment-list-title").after(html);
            });
        }, function (ret) {
            console.log(ret);
        });
        return false;
    });
    //回复评论
    form.on('submit(form_reply)', function (data) {
        var elements = $(data.elem).parents('.main-comment-list-box').find('.main-comment-list-box-children');
        var form_box = $(data.elem).parents('.main-comment-list-box-reply')
        console.log(elements);
        let options = {
            data: {data: base64.encode(my_form.stringify(data.field))},
            method: 'post',
            url: Config.comment_publish,
        }
        my_form.request(options, function (ret) {
            let tpl = $("#comment-children-tpl").html();
            console.log(ret);
            laytpl(tpl).render({data: ret}, function (html) {
                elements.prepend(html);
            });
            form_box.remove();
        }, function (ret) {
            console.log(ret);
        });
        return false;
    });

    //初始加载
    get_comment_list(1);

    $(document).on('click', '#load-more', function () {
        let page = $(this).attr('data-next-page');
        get_comment_list(page);

    });

    //获取评论列表
    function get_comment_list(page = 1) {
        let goal_id = $('input[name="goal_id"]').val();
        let goal_type = $('input[name="goal_type"]').val();
        let options = {
            data: {goal_type: goal_type, goal_id: goal_id, page: page},
            method: 'get',
            url: Config.comment_list,
        };
        //模板
        let tpl = $('#comment-list-tpl').html();

        my_form.request(options, function (ret) {
            let result = ret.next_page > ret.max_page;
            let classes = result == true ? 'layui-hide' : '';
            $("#load-more").attr('data-next-page', ret.next_page).attr('data-max-page', ret.max_page).addClass(classes);
            laytpl(tpl).render({data: ret}, function (html) {
                $("#comment-list-footer").before(html);
            });
        }, function (ret) {

        });
    }

})
